import  flexArgs from "./index.js"
import  assert from "assert"

function testArgs(){
	let { count, name , callback ,options,payload } = flexArgs([
       { name:String },
       { count:Number }, 
       { name:String,options:Object},
       { name:String,callback:Function},
       { name:String,payload:'any'},
       { name:String,callback:Function,options:Object},
    ],arguments,
    {
        name:"tom",
        count:0,
        options:{x:0,z:99}
    })
    // console.log("count=",count,"payload=",typeof(payload)=="function" ?  'function' : JSON.stringify(payload)   ,"name=",name,"callback=",typeof(callback),"options=",JSON.stringify(options))
    // console.log("-----------------------------------")
    return  { count:count,  name:name , callback:callback ,options:options,payload:payload }
}
testArgs({callback:function(){}})


function assertResults(results,expects){
    for(let [key,value] of Object.entries(results)){
        if(typeof(value)==="object"){
            let ok=true
            for(let [k,v] of Object.entries(value)){
                ok=value[k]==expects[key][k]
                if(ok==false) break
            }
            assert.equal(ok,true,`[${key}]不匹配，预期为${JSON.stringify(expects[key])}},结果为${JSON.stringify(value)}`)
        }else if(typeof(value)==="function"){
            assert.equal(typeof(value),"function",`[${key}]不匹配，预期为function,结果为${typeof(value)}`)
        }else{
            assert.equal(value,expects[key],`[${key}]不匹配，预期为${expects[key]},结果为${value}`)
        }       
    }
    console.log("[OK] ",JSON.stringify(expects),"=",JSON.stringify(results))
}


assertResults(testArgs("aaa"),{ count:0, name:"aaa", callback:undefined ,options:{x:0,z:99},payload:undefined })
assertResults(testArgs(100),{ count:100, name:"tom", callback:undefined ,options:{x:0,z:99},payload:undefined })
assertResults(testArgs("bbb",function(){}),{ count:0, name:"bbb", callback:Function ,options:{x:0,z:99},payload:undefined })
assertResults(testArgs("ccc",{x:1}),{ count:0, name:"ccc", callback:undefined ,options:{x:1,z:99},payload:undefined })
assertResults(testArgs("ccc",11111),{ count:0, name:"ccc", callback:undefined ,options:{x:0,z:99},payload:11111 })
assertResults(testArgs("ddd",function(){},{y:1}),{ count:0, name:"ddd", callback:Function ,options:{x:0,y:1,z:99},payload:undefined })
assertResults(testArgs("eee",function(){},{z:1}),{ count:0, name:"eee", callback:Function ,options:{x:0,y:1,z:1},payload:undefined })
assertResults(testArgs({name:"fff",count:88}),{ count:88, name:"fff", callback:undefined ,options:{x:0,z:99},payload:undefined })
assertResults(testArgs("ggg",true),{ count:0, name:"ggg", callback:undefined ,options:{x:0,z:99},payload:true })

 
 


console.log("************************************")

function testArgs2(){
	let { event, payload,msg } = flexArgs([ 
       { event:String,payload:'any'}
    ],arguments) 
    return { event:event, payload:payload,msg:msg } 
}
assertResults(testArgs2("aaa","11111"),{ event:"aaa", payload:"11111",msg:undefined })
assertResults(testArgs2("aaa",true),{ event:"aaa", payload:true,msg:undefined })
assertResults(testArgs2("aaa",2),{ event:"aaa", payload:2,msg:undefined })
assertResults(testArgs2("aaa",{x:1}),{ event:"aaa", payload:{x:1},msg:undefined })

console.log("************************************")

function testArgs3(){
    let { event, payload,count } = flexArgs([              
        {event:String,count:Number},
        {event:String,payload:'any'},        
     ],arguments,
     {
         count:1
     })
     return { event:event, payload:payload,count:count } 
}
assertResults(testArgs3("aaa",2),{ event:"aaa", count:2,payload:undefined })
assertResults(testArgs3("aaa",true),{ event:"aaa", count:1,payload:true })
assertResults(testArgs3("aaa","wxzhang"),{ event:"aaa", count:1,payload:"wxzhang" })
assertResults(testArgs3("aaa",{x:1}),{ event:"aaa", count:1,payload:{x:1} })
// testArgs3("a",1)
// testArgs3("a",true)
// testArgs3("a","wxzhang")
// testArgs3("a",{})

console.log("************************************")

function testArgs4(){
    let { pk , pklist ,x} = flexArgs([              
        {pklist: Array},
        {pk: String},
        {x: Object},         
     ],arguments)
     return {pk,pklist,x } 
}
assertResults(testArgs4({a:1}),{ x:{a:1}})
assertResults(testArgs4([1,2,3]),{ pklist:[1,2,3] })
assertResults(testArgs4("a"),{ pk:"a"})

